var request = window.indexedDB.open('testDB01', '4');
var db;
request.onsuccess = function (event) {
	db = event.target.result;
	console.log('数据库打开成功');
};
// 建表必须在 onupgradeneeded,不能在 onsuccess
// 在 chrome 下,onupgradeneeded 有可能不触发,更把 version 更新为更高版本
request.onupgradeneeded = (event) => {
	db = event.target.result;
	console.log('onupgradeneeded');
	if (!db.objectStoreNames.contains('userList2')) {
		console.log('createIndex')
		var objectStore = db.createObjectStore('userList2', { keyPath: 'id' });
		objectStore.createIndex('username', 'username', { unique: false });
		objectStore.createIndex('age', 'age', { unique: false });
	}
}

const axiosObj = {
	// 新增单个
	addItem: function(item, callback) {
		var req = db.transaction(['userList2'], 'readwrite')
		.objectStore('userList2')
		.add({ id: item.id, username: item.username, age: item.age});

		req.onsuccess = function (event) {
			console.log('数据写入成功');
			callback && callback({
				flag: '0000',
				msg: 'success'
			})
		};
		req.onerror = function (event) {
			console.log('数据更新失败');
			callback && callback({
				flag: '9999',
				msg: 'fail'
			})
		}
	},
	// 编辑单个
	editItem: function(item, callback) {
		var req = db.transaction(['userList2'], 'readwrite')
		.objectStore('userList2')
		.put({ id: item.id, username: item.username, age: item.age});

		req.onsuccess = function (event) {
			console.log('数据写入成功');
			callback && callback({
				flag: '0000',
				msg: 'success'
			})
		};
		req.onerror = function (event) {
			console.log('数据更新失败');
			callback && callback({
				flag: '9999',
				msg: 'fail'
			})
		}
	},
	// 删除单个
	delItem: function(id, callback) {
		var request = db.transaction(['userList2'], 'readwrite')
			.objectStore('userList2')
			.delete(id);

		request.onsuccess = function (event) {
			console.log('数据删除成功');
			callback && callback({
				flag: '0000',
				msg: 'success'
			});
		};
	},
	// 获取列表
	getList: function(callback) {
		var objectStore = db.transaction('userList2').objectStore('userList2');
		var listData = [];

		objectStore.openCursor().onsuccess = function (event) {
			var cursor = event.target.result;
			
			if (cursor) {
				listData.push({
					id: cursor.key,
					username: cursor.value.username,
					age: cursor.value.age,
				});
				cursor.continue();
			} else {
				callback && callback({
					flag: '0000',
					msg: 'success',
					data: listData
				})
			}
		};
	},
	// 获取单个详情
	getItem: function(id, callback) {
		var transaction = db.transaction(['userList2']);
		var objectStore = transaction.objectStore('userList2');
		var req = objectStore.get(id);

		req.onerror = function(event) {
			console.log('事务失败');
		};

		req.onsuccess = function( event) {
			if (req.result) {
				callback && callback({
					flag: '0000',
					msg: 'success',
					data: {
						id: req.result.id,
						username: req.result.username,
						age: req.result.age,
					}
				});
			} else {
				console.log('未获得数据记录');
			}
		};
	}
};